[ `ps |grep $0|grep -v grep|wc -l ` -gt 2 ] && echo "$0 has already run!" && exit 1
echo "          =========  check gost proxybegin:      "`date '+%Y.%m.%d %H:%M:%S'`  =====  >>/tmp/gost.log

cd /tmp

rm ss.ini 2>/dev/null
serverurl="http://ggcloud.tk/mac/"
url="https://www.youtube.com/intl/zh-CN/yt/about/"

curl -o ss.txt -s -l -k  -m 30 https://gitee.com/yzqiang/autoss/raw/master/gost.txt	
if [ $? = 0 ] ; then
tr -d "\015" <ss.txt | while read str
do
  [ "$str" = "" ] && continue
  [ "${str:0:1}" = "#" ]   && continue
  echo "$str" >>ss.ini
done 
fi

[ ! -s ss.ini ] && exit 1

###################### set gost information ####################################


############以下为测速

###############检查proxy可用性

rm ss.txt >/dev/null 2>&1

options1=""
options2=""
ss_usage=""
ss_usage_json=""
rm ss.txt >/dev/null 2>&1
rm ssr.inf >/dev/null 2>&1
rm ssr.ini >/dev/null 2>&1
rm ss.inf >/dev/null 2>&1
rm s.inf >/dev/null 2>&1

SERVER0=""
CC=1
BESTTIME=0
BT=0
BESTTIME0=900   #两秒内的成功个数
BT0=300        	#一秒内的成功个数  
CC0=9900			#扫描条数限制
[ $(date +%H) -ge 7 ] && BESTTIME0=300 && BT0=100  && CC0=2000        #七点钟之后减少扫描个数

HOST0=""


cat ss.ini | while read str
do
[ $CC -ge $CC0 -o $BESTTIME -ge $BESTTIME0 -o $BT -ge $BT0 ] && break
[ "$str" = "" ] && continue
[ ${str:0:1} = "#" ] && continue 
[ ${str:0:1} = "=" ] && continue 
[ ${str:0:1} = "" ] && continue 

#url="https://www.youtube.com/intl/zh-CN/yt/about/"
starttime=$(cat /proc/uptime | cut -d" " -f1)

for i in $(seq 1 5)
do 
rm /tmp/tmp.txt 2>/dev/null
curl  -s -o /tmp/tmp.txt  --proxy-insecure -x "$str" -k  -L -r 0-39999 -m 9  $url  2>/dev/null
CODE="$?"
[  ! "$CODE" == "0" ] && break
done

if [  "$CODE" == "0" ] ; then
  endtime=$(cat /proc/uptime | cut -d" " -f1)  
  TIME=`awk -v x=$starttime -v y=$endtime 'BEGIN {printf "%.2f\n",y-x}'`
  TIME0=`awk -v x=$TIME 'BEGIN{printf "%.2f\n",x-0.01}'`

  [ "${TIME0:0:1}" \< "2.0" ] && let BESTTIME=$BESTTIME+1
	[ "${TIME0:0:3}" \< "1.0" ] && let BT=$BT+1
	
  [ $CC -ge 10 ] && echo "$CC $TIME0 $str" 
  [ $CC -lt 10 ] && echo "0$CC $TIME0 $str" 
  
  echo "$TIME0 $str" >>ss.txt  
  let CC=$CC+1	
else
   TIME0="99.99"
	echo "XX" $TIME0 "$str"
fi
done

[ ! -s ss.txt ] && echo “not valid proxy server” && exit 1


proxy=""
sort ss.txt | head -n 10 >>/tmp/gost.log
sort ss.txt | head -n 10 >/tmp/sss.txt
 while read str
do
  SERVER=`echo "$str"|awk -F ' ' '{print $2}'`
  proxy="$proxy""$SERVER""; "
 # echo $proxy
done </tmp/sss.txt
proxy="$proxy""http smccb.tk:8090; http yzqiang.tk:8090; DIRECT"
proxy=${proxy//:\/\// }

serverurl="http://yzqiang.tk:800/"

curl -o proxy.pac  -s  -l -k  -m 30 $serverurl"proxy.pac"
#curl -o proxy.pac https://www.gitee.com/yzqiang/autoss/raw/master/proxy.pac
sed -i 's/\r//g' proxy.pac
sed -i '/var proxy=/d' proxy.pac
STR="sed -i '1ivar proxy=\"$proxy\";' proxy.pac"
echo $STR >/tmp/setproxy.sh
sh /tmp/setproxy.sh

 curl  -s -m 30 $serverurl"getfile.php?fn=proxy.pac" -F "file=@proxy.pac"
 
 
 
SERVER=`sort ss.txt | head -n 1|awk -F ' ' '{print $2}'`  




echo "killall -9 gost >/dev/null 2>&1" >/etc/config/gost.sh
echo "sleep 2" >>/etc/config/gost.sh
echo 'nice -n -15 gost -L="http://:8090" -L="ss://rc4-md5:yzqyzq1234@:8388" -L="socks5://:10800" -F="'$SERVER'?dns=127.0.01:5556" 2>>/dev/nul &'>>/etc/config/gost.sh

chmod a+x /etc/config/gost.sh
/etc/config/gost.sh

ps|grep gost




 



